<template>
  <div class="current-root">
    <div class="location">
      <div class="city" @click="cityClick">{{ currentCity.cityName }}</div>

      <div class="position" desc="1、位置">
        <span class="text" @click="positionClick">我的位置</span>
        <img src="@/assets/image/home/icon_location.png" alt="icon_location"/>
      </div>
    </div>
    <div class="date-rang section" desc="2、入住日期">
      <div class="start">
        <div class="date">
          <span class="tip">入住</span>
          <span class="time" @click="calendarShow = true">{{ startDate }}</span>
        </div>
        <div class="stay">
          (共{{ stayDate }}晚)
        </div>
      </div>

      <div class="end">
        <div class="date">
          <span class="tip">离店</span>
          <span class="time" @click="calendarShow = true">{{ endDate }}</span>
        </div>
      </div>
    </div>
    <van-calendar v-model:show="calendarShow"
                  type="range"
                  :formatter="formatter"
                  color="#ff9854"
                  @confirm="onConfirm"/>

    <div class="section price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>
    <div class="section keyword bottom-gray-line">关键字/位置/名宿名</div>
    <div class="section hot-suggest" desc="热门建议">
      <template v-for="(item, index) in cityStore?.hotSuggests" :key="index">
        <div class="tag"
             :style="{color: item.tagText.color, background: item.tagText.background.color}"
        >
          {{ item.tagText.text }}
        </div>
      </template>

    </div>
    <div class="section search">
      <div class="search-btn" @click="search">开始搜索</div>
    </div>
  </div>
</template>

<script setup>
import {useRouter} from "vue-router";
import {useCityStore} from "@/stores/modules/city";
import {ref} from "vue";
import {u_date_differ, u_dateAdd, u_formatMonthDay} from "@/utils/format_date";
import {storeToRefs} from "pinia";

const router = useRouter();
const cityStore = useCityStore();
const {currentCity} = storeToRefs(cityStore);
// 调用pinia 的action，请求数据
cityStore.fetchHotSuggestsData()

// 手动选择城市
function cityClick() {
  console.log('city点击')
  router.push('./city')
}

// 点击定位
function positionClick() {
  // 浏览器对象 navigator
  navigator.geolocation.getCurrentPosition(
      res => {
        console.log('获取位置成功')
        console.log(res)
      },
      err => {
        console.log('获取位置成功')
        console.log(err)
      }, {
        timeout: 3000,
        enableHighAccuracy: true,
        maximumAge: 0
      })
}

// 默认的展示日期
const nowDate = new Date()
const tomorrow = u_dateAdd(nowDate, 1, 'day');
// 日期开始结束时间
const startDate = ref(u_formatMonthDay(nowDate))
const endDate = ref(u_formatMonthDay(tomorrow))
// 是否显示日期选择
const calendarShow = ref(false)
const stayDate = ref(1)

// 日期点击确认
function onConfirm(values) {
  const [start, end] = values;
  calendarShow.value = false;
  startDate.value = u_formatMonthDay(start)
  endDate.value = u_formatMonthDay(end)
  stayDate.value = u_date_differ(end, start)
}

// 自定义日期选择的提示文本
function formatter(day) {
  const month = day.date.getMonth() + 1;
  const date = day.date.getDate();

  if (day.type === 'start') {
    day.bottomInfo = '入住';
  } else if (day.type === 'end') {
    day.bottomInfo = '离店';
  }
  return day;
}

function search() {
  router.push({
    path: '/search',
    query: {
      startDate: startDate.value,
      endDate: endDate.value,
      currentCity: currentCity.value.cityName
    }
  })
}
</script>

<style lang="less" scoped>

.current-root {
  .location {
    display: flex;
    align-items: center; // 交叉轴居中
    height: 44px;
    padding: 0 20px;

    .city {
      flex: 1;
      color: #333333;
      font-size: 15px;
    }

    .position {
      width: 74px;
      display: flex;
      align-items: center;

      .text {
        position: relative;
        top: 2px;
        color: #666;
        font-size: 12px;
      }

      img {
        margin-left: 4px;
        width: 18px;
        height: 18px;
      }
    }
  }

  .section {
    display: flex;
    flex-wrap: wrap;
    align-items: center; // 交叉轴中心排列
    padding: 0 20px;
    color: #999;
    height: 44px;

    .start {
      flex: 1;
      display: flex;
      height: 44px;
      align-items: center;
    }

    .stay {

    }

    .end {
      min-width: 30%;
      padding-left: 20px;
    }

    .date {
      display: flex;
      flex-direction: column;

      .tip {
        font-size: 12px;
        color: #999;
      }

      .time {
        margin-top: 3px;
        color: #333;
        font-size: 15px;
        font-weight: 500;
      }
    }
  }

  .date-rang {
    height: 44px;

    .stay {
      flex: 1;
      text-align: center;
      font-size: 12px;
      color: #666;
    }
  }

  .price-counter {

    .start {
      border-right: 1px solid var(--line-color);
    }
  }

  .hot-suggest {
    margin: 5px 0;
    height: auto;

    .tag {
      padding: 4px 8px;
      margin: 5px 3px;
      border-radius: 14px;
      font-size: 12px;
      line-height: 1;
    }
  }

  .search {

    .search-btn {
      width: 342px;
      height: 38px;
      max-height: 50px;
      font-weight: 500;
      font-size: 18px;
      line-height: 38px;
      text-align: center;
      border-radius: 20px;
      color: #fff;
      background-image: var(--theme-liner-gradient);
    }
  }
}

</style>